package com.ayu.scalalearn.chapter07

/**
  * @Author 徐林玉
  * @Description Test01Imutable
  * @Date 2022/8/2 16:16
  * @Version 1.0
  */
object Test01Immutable {

  def main(args: Array[String]): Unit = {


    //不可变数组的基本方式
    //声明
    val arr: Array[Int] = new Array[Int](5)
    //声明2
    val arr2: Array[Int] = Array(1, 2, 3, 4, 5)


    //2、访问元素
    println(arr2(1))

    //3、数组的遍历
    //1)、普通for循环
    for (i <- 0 until arr2.length) {

      println(arr2(i))
    }

    println("========================")
    println("========================")


    for (i <- arr2.indices) {

      println(arr2(i))
    }


    println("========================")
    println("========================")
    //2)、增强for循环
    for (elem <- arr2) {

      println(elem)
    }

    println("========================")
    println("========================")

    //3)、迭代器
    val iter = arr2.iterator

    while (iter.hasNext) {

      println(iter.next())
    }

    println("========================")
    println("========================")

    //4)、使用foreach方法

    arr2.foreach(x => println(x))

    arr2.foreach(println)

    println(arr2.mkString("__"))

    //添加元素
    //在后面添加
    println("========================")
    println("========================")
    val newArr = arr2.:+(99)
    println(arr2.mkString("___"))
    println(newArr.mkString("_"))

    //在前面添加
    println("========================")
    println("========================")
    val newArr2 = newArr.+:(999)
    println(newArr2.mkString("---"))


    //另外的写法
    val newArr3 = newArr2 :+ 90

    println(newArr3.mkString("***"))


    val newArr4 = 11 +: 22 +: newArr3 :+ 33 :+ 44

    println("newArr4:" + newArr4.mkString(","))


  }

}
